package com.zhuge_new.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Adapter.AllplasnAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.AllplanBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;

/**
 * Created by lenovo on 2018-09-26.
 */

public class AllplanActivity extends BaseActivity {

    private String cardId;
    private RecyclerView appplan_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allplan);
        Intent intet=getIntent();
        cardId = intet.getStringExtra("cardId");
        init_title();
        getData();
    }

    @Override
    public void init_title() {
        super.init_title();
        appplan_list = findViewById(R.id.appplan_list);
        appplan_list.setLayoutManager(new LinearLayoutManager(this));
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.repaymentplan_datas)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext,"token"))
                .params("cardId",cardId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        final AllplanBean allplanBean=(AllplanBean) new  DataParse().getBean(response.body(),AllplanBean.class,0);
                        AllplasnAdapter allplasnAdapter=new AllplasnAdapter(baseContext,allplanBean);
                        appplan_list.setAdapter(allplasnAdapter);
                        allplasnAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                if (view.getId()==R.id.all_particulars){
                                    Intent intent=new Intent(AllplanActivity.this,PlandetailsActivity.class);
                                    intent.putExtra("repaymentplanId",allplanBean.getRepayments().get(position).getRepaymentplanId());
                                    startActivity(intent);
                                }else if (view.getId() == R.id.all_delete){
                                    delete(allplanBean.getRepayments().get(position).getRepaymentplanId());
                                }
                            }
                        });
                    }
                });
    }

    private void delete(final String repaymentplanId) {
        BounceTopEnter mBasIn = new BounceTopEnter();
        SlideBottomExit mBasOut = new SlideBottomExit();
        final NormalDialog dialog = new NormalDialog(baseContext);
        dialog.content("您确定要删除此计划吗")
                .style(NormalDialog.STYLE_TWO)
                .titleTextSize(23)
                .showAnim(mBasIn)
                .dismissAnim(mBasOut)
                .show();
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        execute_delete(repaymentplanId);
                        dialog.dismiss();
                    }
                });
    }

    private void execute_delete(String repaymentplanId) {
        OkGo.<String>post(BaseHttp.remove_repaymentplan)
                .tag(this)
                .headers("token",PreferencesUtils.getString(baseContext,"token"))
                .params("repaymentplanId",repaymentplanId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        MToast.makeTextShort(baseContext,msg).show();
                    }
                });
    }
}
